package com.thon.service.project.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.util.HighCharts;
import com.thon.dao.util.HighCharts.PieSeries;
import com.thon.dao.util.Page;
import com.thon.entity.project.Data;
import com.thon.service.project.DataAnalysisService;
import com.thon.service.util.impl.DatabaseAnalysisService;
import com.thon.service.util.impl.DatabaseDateAnalysisImpl;
import com.thon.service.util.impl.HighchartsAnalysisResultImpl;

@Service
@Transactional
public class DataAnalysisServiceImpl extends DatabaseAnalysisService<Data> implements DataAnalysisService<Data> {	
	
			
	
	@Override
	public HighCharts getColumn(Date startDate, Date endDate, String ids) {			
	

		setStartDate(startDate);
		setEndDate(endDate);
		setIds(ids);	
	
		readData();
		
		if (getDatum() != null) {
			
			setAnlaysis(new DatabaseDateAnalysisImpl(startDate,endDate,ids));
			analyseData();
			
			setResult(new HighchartsAnalysisResultImpl(startDate,endDate,getIdsMap(ids),getAnlaysis().getMethod(),getAnlaysis().getStartToEndlength()));			
			writeResult();
		}		
			
		return super.getHighCharts();
	}

	

	
	private Integer findIndex(String str[], String strQuery) {
		for(int i=0;i<str.length;i++)
		{
			if(str[i].equals(strQuery))
				return i; 
		}
		return -1;
	}


	@Override
	public HighCharts pieCharts(Date startDate, Date endDate, String ids) {

		HighCharts highCharts = new HighCharts();			
		if(getDatum() !=null){
		Double totle =0.0;
		if (getDatum()  != null) {
			String[] strArray = ids.split(",");
			
				PieSeries[] series = new PieSeries[strArray.length];
				
				for(int i=0;i<strArray.length;i++)
				{
					series[i] = highCharts.new PieSeries();			
						
				}
				
				Double[] seriesData = new Double[strArray.length];								
					
				
				for (int i = 0; i < seriesData.length; i++)					
					 {
						 seriesData[i]=0.0;
					 }			
				
				for (Data data : getDatum() ) {					
						
						if (null != data.getType()) {
							int ind = findIndex(strArray, data.getType()
									.getId().toString());
							if (ind != -1) {
								seriesData[ind] += data.getValue();		
								totle += data.getValue();								
							}
						}				
					
				
				}
				
				for (int i = 0; i < strArray.length; i++) {
					series[i].setY(seriesData[i]);
					}
				
				highCharts.setPieSeries(series);		
				highCharts.setTotle(totle);
		}
		}
		return highCharts;
	}


	@Override
	public List<Data> getList(Date startDate, Date endDate, String ids) {		
		
		return getDatum() ;
	}


	@Override
	public Page<Data> getFilterPage(Date startDate, Date endDate, String ids,
			Integer pageNo, Integer pageSize) {
		setStartDate(startDate);
		setEndDate(endDate);
		setIds(ids);

		setPageDatum(new Page<Data>());
		getPageDatum().setPageNo(pageNo);
		getPageDatum().setPageSize(pageSize);
		readDataPage();

		return getPageDatum();
	}






}